// 配置服务 - 用于读取和管理应用配置
export interface AttackChainConfig {
  targetPort: number
  targetHost: string
  protocol: string
  endpoints: {
    sendAttackData: string
    getStatus: string
    startAttack: string
    stopAttack: string
    sortNodes: string
  }
  timeout: number
  retryCount: number
}

export interface NetworkSurveyConfig {
  targetPort: number
  targetHost: string
  protocol: string
  endpoints: {
    submitScanTask: string
    getScanStatus: string
    getScanResults: string
  }
  timeout: number
  retryCount: number
}

export interface AppConfig {
  attackChain: AttackChainConfig
  networkSurvey?: NetworkSurveyConfig
  description: string
  version: string
}

class ConfigService {
  private config: AppConfig | null = null
  private configPromise: Promise<AppConfig> | null = null

  /**
   * 获取应用配置
   * @returns Promise<AppConfig>
   */
  async getConfig(): Promise<AppConfig> {
    if (this.config) {
      return this.config
    }

    if (this.configPromise) {
      return this.configPromise
    }

    this.configPromise = this.loadConfig()
    this.config = await this.configPromise
    return this.config
  }

  /**
   * 从配置文件加载配置
   * @returns Promise<AppConfig>
   */
  private async loadConfig(): Promise<AppConfig> {
    try {
      const response = await fetch('/config.json')
      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`)
      }
      const config = await response.json()
      console.log('配置文件加载成功:', config)
      return config
    } catch (error) {
      console.error('加载配置文件失败:', error)
      // 返回默认配置
      return this.getDefaultConfig()
    }
  }

  /**
   * 获取默认配置
   * @returns AppConfig
   */
  private getDefaultConfig(): AppConfig {
    return {
      attackChain: {
        targetPort: 8080,
        targetHost: 'localhost',
        protocol: 'http',
        endpoints: {
          sendAttackData: '/api/attack/chain',
          getStatus: '/api/attack/status',
          startAttack: '/api/attack/start',
          stopAttack: '/api/attack/stop',
          sortNodes: '/api/nodes/sort'
        },
        timeout: 5000,
        retryCount: 3
      },
      networkSurvey: {
        targetPort: 8080,
        targetHost: 'localhost',
        protocol: 'http',
        endpoints: {
          submitScanTask: '/api/survey/scan',
          getScanStatus: '/api/survey/status',
          getScanResults: '/api/survey/results'
        },
        timeout: 10000,
        retryCount: 3
      },
      description: 'DNS链式攻击前端配置文件',
      version: '1.0.0'
    }
  }

  /**
   * 获取攻击链配置
   * @returns Promise<AttackChainConfig>
   */
  async getAttackChainConfig(): Promise<AttackChainConfig> {
    const config = await this.getConfig()
    return config.attackChain
  }

  /**
   * 获取网络扫描配置
   * @returns Promise<NetworkSurveyConfig>
   */
  async getNetworkSurveyConfig(): Promise<NetworkSurveyConfig> {
    const config = await this.getConfig()
    return config.networkSurvey || this.getDefaultConfig().networkSurvey!
  }

  /**
   * 获取目标URL
   * @returns Promise<string>
   */
  async getTargetUrl(): Promise<string> {
    const attackConfig = await this.getAttackChainConfig()
    return `${attackConfig.protocol}://${attackConfig.targetHost}:${attackConfig.targetPort}`
  }

  /**
   * 获取网络扫描目标URL
   * @returns Promise<string>
   */
  async getSurveyTargetUrl(): Promise<string> {
    const surveyConfig = await this.getNetworkSurveyConfig()
    return `${surveyConfig.protocol}://${surveyConfig.targetHost}:${surveyConfig.targetPort}`
  }

  /**
   * 获取发送攻击数据的完整URL
   * @returns Promise<string>
   */
  async getSendAttackDataUrl(): Promise<string> {
    const attackConfig = await this.getAttackChainConfig()
    // 使用代理路径避免CORS问题
    return attackConfig.endpoints.sendAttackData
  }
}

// 导出单例实例
export const configService = new ConfigService()

